﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;

namespace Maticsoft.Web
{
    public partial class UserComments : PageBaseMember
    {
        int _OrderId;
        protected void Page_Load( object sender, EventArgs e )
        {
            Check();

            int.TryParse(Request.QueryString["orderId"], out _OrderId);

            if (!IsPostBack)
            {
                GetProduct();
            }
        }

        protected void GetProduct()
        {
            BLL.Orders ordBLL = new BLL.Orders();
            Model.Orders ordModel = ordBLL.GetModel(_OrderId);
            if (ordModel != null && ordModel.UserId.ToString() == this.UserId)
            {
                BLL.Oitems oitBLL = new BLL.Oitems();
                DataSet ds = oitBLL.GetList(1, "orderId=" + _OrderId + " and IsActive=1 and IsNull(IsComment,0)=0", "oitemId asc");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    hidProductId.Value = ds.Tables[0].Rows[0]["ProductId"].ToString();
                    ltlProductName.Text = ds.Tables[0].Rows[0]["ProductName"].ToString();
                }
            }
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            //BLL.Orders ordBLL = new BLL.Orders();
            //Model.Orders ordModel = ordBLL.GetModel(_OrderId);
            //if (ordModel != null && ordModel.UserId.ToString() == this.UserId)
            //{
            //    BLL.Oitems oitBLL = new BLL.Oitems();
            //    DataSet ds = oitBLL.GetList("orderId=" + _OrderId + " and productId=" + _ProductId);
            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        Model.Comments comModel = new Model.Comments();
            //        BLL.Comments comBLL = new BLL.Comments();
            //        comModel.CreateTime = DateTime.Now;
            //        comModel.Detail = txtComments.Text.Trim();
            //        comModel.IsActive = 1;
            //        comModel.Point = int.Parse(txtPoints.Text.Trim());
            //        comModel.ProductId = _ProductId;
            //        comModel.ShowStatus = 0;
            //        comModel.UserId = int.Parse(this.UserId);
            //        comModel.UpdateTime = DateTime.Now;
            //        comBLL.Add(comModel);
            //        Common.MessageBox.ShowAndRedirect(this.Page, "感谢您的评论", "UserOrder.aspx");
            //    }
            //}
            int productId = 0;
            int.TryParse(hidProductId.Value, out productId);
            if (productId > 0)
            {
                Model.Comments comModel = new Model.Comments();
                BLL.Comments comBLL = new BLL.Comments();
                comModel.CreateTime = DateTime.Now;
                comModel.Detail = txtComments.Text.Trim();
                comModel.IsActive = 1;
                comModel.Point = int.Parse(txtPoints.Text.Trim());
                comModel.ProductId = productId;
                comModel.ShowStatus = 0;
                comModel.UserId = int.Parse(this.UserId);
                comModel.UpdateTime = DateTime.Now;
                comBLL.Add(comModel);
                BLL.Oitems oitBLL = new BLL.Oitems();
                DataSet dss = oitBLL.GetList("orderId=" + _OrderId + " and productId=" + productId);
                Model.Oitems oitModel = oitBLL.GetModel(DigitHelper.GetInt32(dss.Tables[0].Rows[0]["OitemId"]));
                oitModel.UpdateTime = DateTime.Now;
                oitModel.IsComment = 1;
                oitBLL.Update(oitModel);
                DataSet ds = oitBLL.GetList(1, "orderId=" + _OrderId + " and IsActive=1 and IsNull(IsComment,0)=0", "oitemId asc");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Common.MessageBox.ShowAndRedirect(this.Page, "评论成功，请继续评论其余产品", "UserComments.aspx?orderid=" + _OrderId);
                }
                else
                {
                    BLL.Orders ordBLL = new BLL.Orders();
                    Model.Orders ordModel = ordBLL.GetModel(_OrderId);
                    ordModel.Status = 6;
                    ordModel.UpdateTime = DateTime.Now;
                    ordBLL.Update(ordModel);
                    Common.MessageBox.ShowAndRedirect(this.Page, "评论完成，请等待审核", "UserOrder.aspx");
                }
            }
        }
    }
}